﻿using Dapper;
using System;
using BCEntity.OA.WorkflowTemplate.WorkflowTemplateStepFormItem;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowTemplate.WorkflowTemplateStepFormItem
{
	/// <summary>
	/// 工作流模板步骤表单项
	/// </summary>
	public class WorkflowTemplateStepFormItemData : IWorkflowTemplateStepFormItemData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowtemplatestepformitem";
		private const string QUERY = @"SELECT WorkflowTemplateStepFormItemId,SettingElements,CreateAdminId,CreateAdminName,CreateTime,EditAdminId,EditAdminName,UpdateTime,FormConfigTitle,
											FormConfigCode,FormItemCode,ItemTitle,ItemHeight,ItemOrder,ItemType,PreviewType,FormDatabase,FormTable,BoundField
										FROM oa_workflowtemplatestepformitem";
		public WorkflowTemplateStepFormItemData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作流模板步骤表单项是否存在
		public async Task<bool> Exists(long workflowTemplateStepFormItemId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepFormItemId=?WorkflowTemplateStepFormItemId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormItemId", workflowTemplateStepFormItemId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作流模板步骤表单项
		public async Task<WorkflowTemplateStepFormItemEntity> Add(WorkflowTemplateStepFormItemEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowtemplatestepformitem", new string[] { "SettingElements", "CreateAdminId", "CreateAdminName", "FormConfigTitle", "FormConfigCode",
				"FormItemCode", "ItemTitle", "ItemHeight", "ItemOrder", "ItemType", "PreviewType","FormDatabase","FormTable","BoundField" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepFormItemId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?SettingElements", entity.SettingElements?.Trim(), DbType.String);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormDatabase", entity.FormDatabase?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormTable", entity.FormTable?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?BoundField", entity.BoundField?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormItemCode", entity.FormItemCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ItemTitle", entity.ItemTitle?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?ItemHeight", entity.ItemHeight?.Trim(), DbType.AnsiString, size: 10);
			parameters.Add("?ItemOrder", entity.ItemOrder, DbType.Int32, size: 11);
			parameters.Add("?ItemType", entity.ItemType?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?ItemPreviewType", entity.PreviewType?.Trim(), DbType.AnsiString, size: 50);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormItemEntity>(sql, parameters, databaseContext.Transaction);

		}
		public async Task<bool> Add(List<WorkflowTemplateStepFormItemEntity> entities)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowtemplatestepformitem", new string[] { "SettingElements", "CreateAdminId", "CreateAdminName", "FormConfigTitle", "FormConfigCode",
				"FormItemCode", "ItemTitle", "ItemHeight", "ItemOrder", "ItemType", "PreviewType", "FormDatabase", "FormTable", "BoundField" });
			return await this.databaseContext.Master.ExecuteAsync(sql, entities, databaseContext.Transaction) == entities.Count;

		}
		#endregion

		#region 修改工作流模板步骤表单项
		public async Task<WorkflowTemplateStepFormItemEntity> Update(WorkflowTemplateStepFormItemEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowtemplatestepformitem", new string[] { "SettingElements", "EditAdminId", "EditAdminName", "FormConfigTitle", "FormConfigCode",
				"FormItemCode", "ItemTitle", "ItemHeight", "ItemOrder", "ItemType", "PreviewType", "FormDatabase", "FormTable", "BoundField" }, new string[] { "WorkflowTemplateStepFormItemId" });
			sql = string.Format("{0} {1} WHERE WorkflowTemplateStepFormItemId=?WorkflowTemplateStepFormItemId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormItemId", entity.WorkflowTemplateStepFormItemId, DbType.Int64, size: 20);
			parameters.Add("?SettingElements", entity.SettingElements?.Trim(), DbType.String);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?FormConfigTitle", entity.FormConfigTitle?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormDatabase", entity.FormDatabase?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormTable", entity.FormTable?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?BoundField", entity.BoundField?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?FormItemCode", entity.FormItemCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?ItemTitle", entity.ItemTitle?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?ItemHeight", entity.ItemHeight?.Trim(), DbType.AnsiString, size: 10);
			parameters.Add("?ItemOrder", entity.ItemOrder, DbType.Int32, size: 11);
			parameters.Add("?ItemType", entity.ItemType?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?ItemPreviewType", entity.PreviewType?.Trim(), DbType.AnsiString, size: 50);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormItemEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 删除工作流模板步骤表单项
		public async Task<bool> Delete(long workflowTemplateStepFormItemId)
		{
			string sql = @"DELETE FROM oa_workflowtemplatestepformitem WHERE WorkflowTemplateStepFormItemId=?WorkflowTemplateStepFormItemId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormItemId", workflowTemplateStepFormItemId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		public async Task<bool> Delete(string formConfigCode)
		{
			string sql = @"DELETE FROM oa_workflowtemplatestepformitem WHERE FormConfigCode=?FormConfigCode";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormConfigCode", formConfigCode, DbType.AnsiString);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作流模板步骤表单项
		public async Task<WorkflowTemplateStepFormItemEntity> GetEntity(long workflowTemplateStepFormItemId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowTemplateStepFormItemId=?WorkflowTemplateStepFormItemId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowTemplateStepFormItemId", workflowTemplateStepFormItemId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowTemplateStepFormItemEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板步骤表单项列表
		public async Task<IEnumerable<WorkflowTemplateStepFormItemEntity>> GetList(string formConfigCode)
		{
			string sql = string.Format("{0} WHERE FormConfigCode=?FormConfigCode", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?FormConfigCode", formConfigCode.Trim(), DbType.AnsiString, size: 100);
			return await this.databaseContext.Slave.QueryAsync<WorkflowTemplateStepFormItemEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流模板步骤表单项分页
		public async Task<(IEnumerable<WorkflowTemplateStepFormItemEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowtemplatestepformitem {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowTemplateStepFormItemEntity, int>(sql, parameters);
		}
		#endregion
	}
}



